﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;

namespace UniversityManagementSystem.UI
{
    public partial class CourseAssign : System.Web.UI.Page
    {
        Teacher teacher = new Teacher();
        Course course = new Course();
        List<Teacher> allTeacher = new List<Teacher>();
        List<Course> allCourse = new List<Course>();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Department department = new Department();
                    List<Department> allDepartments = new List<Department>();
                    allDepartments = department.GetAllDepartments();
                    departmentDropDownList.DataSource = allDepartments;
                    departmentDropDownList.DataValueField = "DepartmentCode";
                    departmentDropDownList.DataTextField = "DepartmentName";
                    departmentDropDownList.DataBind();
                    departmentDropDownList_SelectedIndexChanged(null, null);
                    courseTitleDropDownList_SelectedIndexChanged(null, null);
                    teacherDropDownList_SelectedIndexChanged(null, null);
                }
            }
            catch (Exception)
            {
                messageLabel.Text = "Error occured while loading the page.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void departmentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                assignedCreditTextBox.Text = "";
                remainingCreditTextBox.Text = "";
                courseNameTextBox.Text = "";
                courseCreditTextBox.Text = "";
                allTeacher = teacher.GetTeachers(departmentDropDownList.SelectedValue);
                teacherDropDownList.DataSource = allTeacher;
                teacherDropDownList.DataValueField = "TeacherId";
                teacherDropDownList.DataTextField = "TeacherName";
                teacherDropDownList.DataBind();
                ViewState["Teacher"] = allTeacher;
                allCourse = course.GetCourse(departmentDropDownList.SelectedValue);
                courseTitleDropDownList.DataSource = allCourse;
                courseTitleDropDownList.DataValueField = "CourseTitle";
                courseTitleDropDownList.DataTextField = "CourseName";
                courseTitleDropDownList.DataBind();
                ViewState["Course"] = allCourse;
                courseTitleDropDownList_SelectedIndexChanged(null,null);
                teacherDropDownList_SelectedIndexChanged(null,null);
            }
            catch(Exception ex)
            {
                messageLabel.Text = "Unable to load teacher.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
            
        }

        protected void teacherDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                allTeacher = (List<Teacher>)ViewState["Teacher"];
                Teacher findTeacher = allTeacher.Find(delegate(Teacher p) { return p.TeacherId == teacherDropDownList.SelectedValue; });
                if(findTeacher!=null)
                {
                    assignedCreditTextBox.Text = findTeacher.TeacherCredit.ToString();
                    remainingCreditTextBox.Text = findTeacher.TeacherRemainingCredit.ToString();
                    ViewState["selectedTeacher"] = findTeacher;
                }

            }
            catch (Exception exception)
            {
                messageLabel.Text = "Error Occured,Please try again.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
            
        }

        protected void courseTitleDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                allCourse = (List<Course>)ViewState["Course"];
                Course findCourse = allCourse.Find(delegate(Course aCourse) { return aCourse.CourseTitle == courseTitleDropDownList.SelectedValue; });
                if(findCourse!=null)
                {
                    courseNameTextBox.Text = findCourse.CourseName;
                    courseCreditTextBox.Text = findCourse.CourseCredit.ToString();
                    ViewState["selectedCourse"] = findCourse;
                }
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Error Occured,Please try again.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
            
        }

        protected void assignButton_Click(object sender, EventArgs e)
        {
            string assignCourseMessage = "";
            try
            {
                Teacher aTeacher = (Teacher) ViewState["selectedTeacher"];
                Course aCourse = (Course) ViewState["selectedCourse"];
                assignCourseMessage = course.AssignCourse(aTeacher,aCourse);
                messageLabel.Text = assignCourseMessage;
                messageLabel.ForeColor = System.Drawing.Color.Blue;
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Sorry,Course cannot be assigned right now.Please try again later.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}